<?php
defined( '_VALID_MOS' ) or die( 'Restricted access' );
error_reporting (0); 
global $mosConfig_absolute_path;
global $mosConfig_live_site;

if(!class_exists('JEConfig'))
{
    require_once( $mosConfig_absolute_path.'/administrator/components/com_jcs/joomlaequipment.config.php');
}

$_MAMBOTS->registerFunction( 'onAfterStart', 'jcsGrant' );
$_MAMBOTS->registerFunction( 'onAfterStart', 'jcsBAlert' );

function jcsBAlert()
{
   
    error_reporting (0); 
    global $database, $mainframe, $option, $task, $mosConfig_lang, 
		$mosConfig_absolute_path, $mosConfig_live_site, $Itemid;
	
	//$my = $mainframe->getUser();
    //if(!$my->id) return;
    
	//jcsAlertUser($my->email, $sname, 'exp');
    $ex = JEConfig::get('general.expire');

    $sql = "
    SELECT ru.email, ru.name as uname, ru.id as uid, u.id, u.user_id, p.name, a.id AS aid FROM #__jcs_user_subscr AS u
 LEFT JOIN #__jcs_plans AS p ON p.id = u.subscription_id
 LEFT JOIN #__jcs_exp_alerts AS a ON a.user_subscription_id = u.id
 LEFT JOIN #__users AS ru ON ru.id = u.user_id
     WHERE u.extime < CURDATE() + INTERVAL $ex DAY
	 AND u.extime > CURDATE()
	 GROUP BY u.id
     ";
     //echo $sql;
     $database->setQuery( $sql );
     $rows = $database->loadObjectList();
     //print_r($rows);
     if(is_array($rows))
     {
        foreach($rows AS $row)
        {
            if(!$row->aid)
            {
                if(!function_exists('jcsMakeSubscr'))
				{
					require_once( $mosConfig_absolute_path.'/administrator/components/com_jcs/jcs.functions.php');
				}
				$sql = "SELECT params FROM #__mambots WHERE element='jcontentsubscription.granter'";
				$database->setQuery($sql);
				$database->loadObject($mambot);
				$params = new mosParameters( $mambot->params );
				$alert = $params->get('alert');
				if ($alert) 
				{
				    jcsAlertUser($row->email, $row->uname, $row->name, 'exp');
	                $sql = "INSERT INTO #__jcs_exp_alerts (`user_subscription_id`, `user_id`, `stime`) VALUES ('{$row->id}', '{$row->uid}', NOW())";
	                $database->setQuery( $sql );
	                $database->query();
				}
            }
        }
     }
}
function jcsGrant()
{
    error_reporting (0); 
	
	global $database, $mainframe, $option, $task, $mosConfig_lang, 
           $mosConfig_absolute_path, $mosConfig_live_site, $Itemid;
		   
	
    $my = $mainframe->getUser();
    
    //jcsAlertUser($my->email, $sname, 'exp');
    
    if(!$my->id) return;
    
        $query = "SELECT id"
    . "\n FROM #__mambots"
    . "\n WHERE element = 'jcontentsubscription.granter'"
    . "\n AND folder = 'system'"
    ;
    $database->setQuery( $query );
    $id     = $database->loadResult();
    $mambot = new mosMambot( $database );
    $mambot->load( $id );
    $botParams = new mosParameters( $mambot->params );
  
    $regirect = $botParams->def( 'regirect');
    $newuser  = $botParams->def( 'newuser');
    
    $query = "SELECT * "
    . "\n FROM #__jcs_plans WHERE published = 1 AND `grant_new` = 'Y' AND `ctime` < '$my->registerDate'"
    ;
    $database->setQuery( $query );
    $rows = $database->loadObjectList();
    //echo $query;
    //print_r($rows);
    
    foreach($rows AS $row)
    {
        $query = "SELECT COUNT(*) "
        . "\n FROM #__jcs_user_subscr WHERE user_id = $my->id AND subscription_id = $row->id";
        ;
        $database->setQuery( $query );
        $total = $database->loadResult();
        if($total == 0)
        {
            $url = $mosConfig_live_site."/index.php?option=com_jcs";
            if((boolean)trim($row->granturl))
            {
                $url = $mosConfig_live_site."/index.php?option=com_content&task=view&id=".$row->granturl;
            }
            $val['id']      = $row->id;
            $val['gateway'] = '.granter';
            $val['gateway_id'] = time();
            
			if(!function_exists('jcsMakeSubscr'))
			{
				require_once( $mosConfig_absolute_path.'/administrator/components/com_jcs/jcs.functions.php');
			}
		    
            jcsMakeSubscr($my->id, $val, 1, $url);
        }
    }
    
    $query = "SELECT * "
    . "\n FROM #__jcs_plans WHERE published = 1 AND `grant_reg` = 'Y' AND `ctime` > '$my->registerDate'"
    ;
    $database->setQuery( $query );
    $rows = $database->loadObjectList();
    //echo $query;print_r($rows);
    foreach($rows AS $row)
    {
        $query = "SELECT COUNT(*) "
        . "\n FROM #__jcs_user_subscr WHERE user_id = $my->id AND subscription_id = $row->id AND `ctime` >= '$row->ctime'";
        ;
        $database->setQuery( $query );
        $total = $database->loadResult();
        if($total == 0)
        {
            $url = $mosConfig_live_site."/index.php?option=com_jcs";
            if((boolean)trim($row->granturl))
            {
                $url = $mosConfig_live_site."/index.php?option=com_content&task=view&id=".$row->granturl;
            }
            $val['id']      = $row->id;
            $val['gateway'] = '.granter';
            $val['gateway_id'] = time();
            
            if(!function_exists('jcsMakeSubscr'))
			{
				require_once( $mosConfig_absolute_path.'/administrator/components/com_jcs/jcs.functions.php');
			}
			jcsMakeSubscr($my->id, $val, 1, $url);
        }
    }
    
    
}

?>